<#include "${ftl_skin}/common/header.ftl">
    </head>

    <body>
        <div class="ibox-login">
            <div class="form-login">
                <dl class="form-title">
                    <dt><img src="${path.skinPath }img/logo_txt.png" alt=""></dt>
                    <dd>
                        <a href="#"><img src="${path.skinPath }img/logo.png" alt=""></a>
                    </dd>
                </dl>
                <form class="form-horizontal m-t" id="login_form" method="post">
                    <div class="form-ny-box">
                        <ul class="form-list">
                            <li><input type="text" placeholder="请输入用户名" required name="user_name" id="user_name"></li>
                            <li><input type="password" placeholder="请输入密码" required name="user_password" id="user_password"></li>
                            <li>
                                <img id="randomcode-img" src="${path.servicePath }index/randomcode.do" alt="" width="95" onclick="updateCode();">
                                <input type="text" placeholder="验证码" id="random_code" name="random_code">
                            </li>
                            <li>
                                <a href="javascript:void();" onClick="javascript:HFLogin();return false;">登录</a>
                            </li>
                        </ul>
                    </div>
                </form>
                <footer>Copy Right © 2017-2018 广州能源检测研究院版权所有</footer>
            </div>
        </div>

        <#include "${ftl_skin}/common/js.ftl">
            <script type="text/javascript">
                $(function() {
                    if(window.top != window.self) {
                        window.top.location.href = "${path.viewPath }/index/login.do";
                    }

                    $('#login_form')[0].reset();

                    $(document).bind("keypress", function() {
                        if(arguments[0].keyCode === 13) {
                            HFLogin();
                        }
                    });

                    //如果有key传过来就跳到找回密码页面
                    <#if key??>
                        showPsDiv(3); 
                    </#if>

                });

                //显示登陆框
                function showDivLogin() {
                    $("#div_login").show();
                    $("div[id^=password_]").hide();
                }
                //显示找回密码框框
                function showPsDiv(id) {
                    $("#div_login").hide();
                    $("div[id^=password_]").hide();
                    $("#password_" + id).show();

                }

                //发送邮件
                function sendmail() {
                    var cemail = $("#cemail").val();
                    console.log("cemail:" + cemail);
                    if(cemail == "") {
                        HFAlert("请输入邮箱！", "", "warning");
                        return;
                    }
                    HFAjax("${path.servicePath }passwordretrieval/sendmail.do", {
                        email: cemail
                    }, function(res) {
                        if(res.state == 0) {
                            HFToastr("操作成功");
                            showPsDiv(2);
                            $("span[name='input_email']").empty().append(cemail);
                            $("#cemail").val("");
                        } else {
                            HFToastrError("操作失败", res.message);
                        }
                    });
                }

                //更改密码
                function passwordRetrival() {
                    if($("#password_update").val() != $("#password_check").val()) {
                        HFToastrError("密码不一致");
                        return;
                    }
                    var key = '${key! }';
                    var password = $("#password_update").val();
                    HFAjax("${path.servicePath }passwordretrieval/passwordretrieval.do", {
                        key: key,
                        password: password
                    }, function(res) {
                        if(res.state == 0) {
                            HFToastr("操作成功");
                            showPsDiv(4);
                            window.location.href = "${path.viewPath }index/login.do";
                        } else {
                            HFToastrError("操作失败", res.message);
                        }
                    });
                }

                function HFLogin() {

                    if($("#user_name").val() == '') {
                        HFAlert("请输入用户名！", "", "warning");
                        return;
                    }
                    if($("#user_password").val() == '') {
                        HFAlert("请输入密码！", "", "warning");
                        return;
                    }

                    if(!checkCode()) {
                        HFAlert("验证码错误！请重新输入", "", "warning");
                        return;
                    }

                    showHFLoading();
                    $.ajax({
                        url: "${path.servicePath }/index/login.do",
                        async: true,
                        type: 'POST',
                        data: {
                            j_username: $("#user_name").val(),
                            j_password: hex_md5($("#user_password").val()),
                            random_code: $("#random_code").val()
                        },
                        complete: function(XMLHttpRequest, textStatus) {
                            // alert(XMLHttpRequest.responseText); 
                            hideHFLoading();
                        },
                        success: function(msg) {
                            if(msg.state == 0) {
                                window.top.location.href = "${path.viewPath }/admin/index.do";
                            } else if(msg.state == 3) {
                                HFAlert("验证码错误！", "", "warning");
                                updateCode();
                            } else {
                                HFAlert("用户名或密码错误！", "", "error", function() {
                                    window.top.location.href = window.top.location.href;
                                });
                            }
                        }
                    });
                }

                function checkCode() {
                    var success = false;
                    $.ajax({
                        url: "${path.servicePath }/index/codeauth.do",
                        async: false,
                        type: 'POST',
                        data: {
                            code: $("#random_code").val()
                        },
                        dataType: "json",
                        success: function(msg) {
                            if(msg.state == 0) {
                                success = true;
                            } else {
                                HFAlert("验证码错误！", "", "warning");
                                updateCode();
                            }
                        }
                    });
                    return success;
                }

                function updateCode() {
                    $("#randomcode-img").attr("src", "${path.servicePath }index/randomcode.do?time=" + new Date().getTime());
                }
            </script>

    </body>

    </html>